package com.tanhua.app.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.app.intercept.UserHolder;
import com.tanhua.app.service.SettingsService;
import com.tanhua.app.service.UserInfoService;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SettingsService settingsService;

    /**
     * 查询用户信息（读取）
     * @param userId
     * @param token
     * @return
     */
    @GetMapping
    public ResponseEntity findUserInfo(Long userId, @RequestHeader("Authorization")String token){
        //通过拦截器设校验oken
//        //1.校验token
//        boolean verifyToken = JwtUtils.verifyToken(token);
//
//        if (!verifyToken){
//            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
//        }
//
//        //2.获取token中的信息
//        Claims claims = JwtUtils.getClaims(token);
//        final String id = claims.get("id").toString();

        //从ThreadLocal线程中获取存储在其中的数据信息（UserHolder是存储数据的 ）
        Long id = UserHolder.get().getId();
        if (userId == null){
            userId=Long.valueOf(id);
        }
        //3.调用Service查询
        UserInfoVo userInfoVo = userInfoService.findById(userId);

        return ResponseEntity.ok(userInfoVo);
    }


    /**
     * 修改用户信息（更新）
     * @param token
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestHeader("Authorization")String token, @RequestBody UserInfo userInfo){
        //通过拦截器设校验oken
//        //1.校验token
//        boolean verifyToken = JwtUtils.verifyToken(token);
//        if (!verifyToken){
//            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
//        }
//
//        //2.获取token中的信息
//        Claims claims = JwtUtils.getClaims(token);
//        long userId = Long.parseLong(claims.get("id").toString());
        //从ThreadLocal线程中获取存储在其中的数据信息（UserHolder是存储数据的 ）
        Long id = UserHolder.get().getId();
        //3.设置用户修改的信息
        userInfo.setId(id);
        userInfoService.updateById(userInfo);

        return ResponseEntity.ok(null);
    }

    /**
     * 通用设置查询功能
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity settings(){
         //1.调用Service层执行业务逻辑
        SettingsVo settingsVo = settingsService.settings();

        //2.返回结果
        return ResponseEntity.ok(settingsVo);
    }


    /**
     * 设置通用设置中的没陌生人问题
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map param){
        //1.获取参数
        String content = (String) param.get("content");

        //2.调用Service层执行业务逻辑
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/notifications/setting")
    public ResponseEntity settings(@RequestBody Map param){
        //1，获取参数，调用Service层执行业务逻辑
        settingsService.saveSettings(param);
        return ResponseEntity.ok(null);

    }

    /**
     * 黑名单分页查询
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity blackList(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer pageSize){
        //1.调用Service执行业务逻辑查询
        PageResult pageResult = settingsService.findBlackList(page,pageSize);
        //2.返回结果
        return ResponseEntity.ok(pageResult);
    }

    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity removeBlack(@PathVariable("uid") Long blackUserId){
        //1.调用Service执行业务逻辑
        settingsService.deleteBlack(blackUserId);

        //2.返回结果
        return ResponseEntity.ok(null);

    }
}
